package com.baor.controller;

import com.baor.service.DeliverService;
import com.baor.util.R;
import com.baor.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/commit")
public class DeliverController {

    @Autowired
    private DeliverService deliverService;

    @Autowired
    private UploadUtil uploadUtil;

    // 添加投递
    @RequestMapping("/add")
    public R add(@RequestBody Map<String, Object> params){
        return deliverService.add(params);
    }
    // 图片上传接口
    @PostMapping("/uploadImage")
    public R uploadImage(@RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) {
                return R.er("上传文件不能为空");
            }
            //
            String filePath = uploadUtil.uploadUtil("/pictures", new MultipartFile[]{file});

            // 返回文件路径给前端
            System.out.println(filePath);
            Map<String, Object> data = new HashMap<>();
            data.put("result", "上传成功");
            data.put("filePath", filePath);

            return R.ok(data);
        } catch (Exception e) {
            e.printStackTrace();
            return R.er("上传失败: " + e.getMessage());
        }
    }

    // 修改投递 - 支持设备ID列表
    @RequestMapping("/update")
    public R update(@RequestBody Map<String, Object> params){
        return deliverService.update(params);
    }


    // 查询所有投递列表
    @RequestMapping("/getList")
    public R getList(@RequestBody Map<String, Object> params){
        return deliverService.getList(params);
    }
    // 根据ID查询单个投递
    @RequestMapping("/get/{id}")
    public R getById(@PathVariable("id") Integer id){
        return deliverService.getById(id);
    }

}